﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using System.Data;
using TimKen.DataAccess;
using TimKen.DataEntity;
namespace TimKen.Web.order
{
    public partial class OrderInfos : TimKen.App.WebPage
    {
        TimKen.BussLogic.OrderManager orderM = new TimKen.BussLogic.OrderManager();
        public PDescriptionManager pdm = new PDescriptionManager();
        TransTypeManager tm = new TransTypeManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {         
                if (CurrentUserName != null && CurrentUserName!="")
                {
                    //直接确认订单，解密订单ID
                    if (Request.QueryString["confirmOrderID"] != null)
                    {
                        string orderid = Tools.DecryptUserName(Request.QueryString["confirmOrderID"]);
                        if (string.IsNullOrEmpty(orderid))
                            Response.Redirect("/Error.aspx");

                        TimKen.DataEntity.Order order = orderM.GetModel(orderid);
                        if (null == order)
                            Response.Redirect("/Error.aspx");

                        orderM.ConfirmOrder(order);
                        Tools.SendOrderConfirmPayment(order.OrderID);
                        Response.Redirect("/Index.aspx");
                    }
                    if (Request.QueryString["OrderID"] != null)
                    {
                        OrderID = Request.QueryString["OrderID"];
                        BindOrder();
                        BindOrderItems();
                    }
                }
                else {
                    Response.Redirect("/Index.aspx");
                }
            }
        }

        protected string OrderID
        {
            get
            {
                if (ViewState["OrderID"] != null)
                    return (ViewState["OrderID"].ToString());
                else
                    return "";
            }
            set
            {
                ViewState["OrderID"] = value;
            }
        }

        protected void BindOrderItems()
        {
            TimKen.BussLogic.OrderItemManager orderItemM = new TimKen.BussLogic.OrderItemManager();
            DataSet orderItems = orderItemM.GetOrderItems(OrderID);
            repOrderItems.DataSource = orderItems;
            repOrderItems.DataBind();
        }

        protected void BindOrder()
        {
            TimKen.DataEntity.Order order = orderM.GetModel(OrderID);
            if (order.Creator != CurrentUserName)
            {
                Response.Redirect("/ErrorAuthority.aspx");
            }
            else
            {
                labBaseAmount.Text = decimal.Round(order.BaseAmount, 2).ToString();
                labBillAddress.Text = order.BillAddress;
                labCardCode.Text = order.CardCode;
                labContractStatus.Text = order.ContractStatus.HasValue ? (order.ContractStatus.Value == 0 ? GetLangString("ForConfirm") : GetLangString("Confirmed")) : "";
                labCreateDate.Text = order.CreateDate.ToShortDateString();
                labCurrency.Text = order.Currency;


                if (order.OrderStatus != TimKen.DataEntity.OrderStatus.ForFirstBridgeSOFreightConfirm && order.Freight.Value == 0)
                    labFreight.Text = GetLangString("CustomerSelf-care");
                else
                {
                    if (order.Freight.HasValue)
                        labFreight.Text = order.Freight.ToString();
                }

                labOrderID.Value = order.OrderID;
                labOrderNo.Text = order.OrderNo;
                labOrderStatus.Text = orderM.GetOrderStatusName(order.OrderStatus, LanguageID);
                labRemark.Text = order.Remark;
                labSalesPersonCode.Text = order.SalesPersonCode.ToString();
                labShipAddress.Text = order.ShipAddress;
                labShipTo.Text = order.Country;


                labExpectDate.Text = order.ExpectDate == null ? "" : order.ExpectDate.Value.ToString("yyyy-MM-dd");
                lblPlanDate.Text = order.PlanDate == null ? "" : order.PlanDate.Value.ToString("yyyy-MM-dd");

                labTaxAmount.Text = decimal.Round(order.TaxAmount, 2).ToString();
                labTaxClass.Text = order.TaxClassID.ToString();
                labTotalAmount.Text = decimal.Round(order.TotalAmount, 2).ToString();
                labTransType.Text = tm.GetModel(order.ShipType.Value, LanguageID).TransTypeName;
                UserAccountDAO da = new UserAccountDAO();
                UserAccount user=da.GetModel(CurrentUserName);
                //TODO:联系人的相关信息显示
                labContactPerson.Text = order.CntctCode;
                labLMName.Text = order.ContactPerson;
                labMobile.Text = user.Phone;
                labTelephone.Text = order.Telephone;
                labAddress.Text = order.Address;
                this.labEmail.Text = user.Email;

                lblTotalAmount2.Text = decimal.Round(order.TotalAmount, 2).ToString();
                lblBaselAmount2.Text = decimal.Round(order.BaseAmount, 2).ToString();

                if (order.OrderStatus == TimKen.DataEntity.OrderStatus.CustomerConfirmed)
                    this.btnConfirm.Visible = true;
                else
                    this.btnConfirm.Visible = false;
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            TimKen.DataEntity.Order Order = orderM.GetModel(OrderID);
            orderM.ConfirmOrder(Order);
            Tools.SendOrderConfirmPayment(Order.OrderID);
            Response.Redirect(Request.RawUrl);
        }
    }
}